﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using App.Data;
using App.Core.Extensions;
using App.Core.DB;


public partial class UserControls_CustomerList : BaseUserControl
{
    private bool _isListOfDevelopers = false;
    //public List<Customer> DataSource { get; set; }
    public DataTable DataSource { get; set; }
    public bool IsListOfDevelopers
    {
        get { return _isListOfDevelopers; }
        set { _isListOfDevelopers = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public override void DataBind()
    {
        BindCustomerList();
        base.DataBind();
    }
    protected void BindCustomerList()
    {
        #region Old Implementation
        //        if (DataSource == null || DataSource.Count == 0)
//            ltrListing.Text = String.Empty;
//        else
//        {
//            String listItemHtml = @"<div class='ListItem'>                                    
//                                    <div class='ListTitle'><a href='/Pages/Public/CompanyProfile.aspx?ID={0}'>{1}</a></div>
//                                    <div class='ListSubTitle'><b>Location:</b> {2}</div>
//                                    <div style='margin-top:5px;'>
//                                        {3}{4}
//                                        <div class='ListBotter'>{5}</div>                
//                                    </div>                                    
//                                </div>";
//            String thumbImage = "<img src='" + AppConstants.CUSTOMER_FILES + "/{0}/{1}' alt='' title='' align='right' class='ThumbInList' />";
//            StringBuilder sb = new StringBuilder(10);
//            foreach (Customer customer in this.DataSource)
//            {
//                String priceHtml = String.Empty;
//                String thumnail = String.Empty;

//                //if (project.PricePerUnitFrom > 0 && project.PricePerUnitTo > 0 & project.ShowPricingInfo)
//                //    priceHtml = String.Format("From {0} To {1}", WebUtil.FormatData(project.PricePerUnitFrom), WebUtil.FormatData(project.PricePerUnitTo));
//                //else
//                //    priceHtml = "Negotiatable";
//                String logo = customer.Logo.IsNullOrEmpty() ? String.Empty : String.Format(thumbImage, "Logo", customer.Logo);
//                String webSite = customer.WebSite.IsNullOrEmpty() ? String.Empty : String.Format("<b>Web Site: </b><span><a href='{0}' target='_blank'>{1}</a></span>", WebUtil.GetCompleteUrl(customer.WebSite), customer.WebSite);
//                sb.AppendFormat(listItemHtml, customer.ID, customer.Name.HtmlEncode(),
//                    String.Format("{0} {1}", "London", String.Empty),
//                    logo, GetDisplayableCustomerInfo(customer),
//                    webSite);
//                ltrListing.Text = sb.ToString();
//            }            
        //        }
        #endregion Old Implementation

        #region New Implementation

        if (DataSource == null || DataSource.Rows.Count == 0)
            ltrListing.Text = String.Empty;
        else
        {
            String listItemHtml = @"<div class='ListItem'>
                                        <table cellpadding='0' cellspacing='0'>
                                            <tr>
                                                <td>
                                                    <div class='ListTitle'><a href='/Pages/Public/CompanyProfile.aspx?ID={0}' target='_blank'>{1}</a></div>
                                                    <div class='ListSubTitle'><b>Location:</b> {2}</div>
                                                    <div style='margin-top:5px;'>
                                                        {4}
                                                        <div class='ListBotter'>{5}</div>                
                                                    </div>
                                                </td>
                                                <td style='width:60px;'>
                                                    {3}
                                                </td>
                                             </tr>               
                                        </table>                                        
                                    </div>
                                    <div class='ListSeparator'><div></div></div>";
            String thumbImage = "<img src='" + AppConstants.CUSTOMER_LOGO + "/{0}' alt='' title='' align='right' class='ThumbInList' />";
            StringBuilder html = new StringBuilder(10);
            foreach (DataRow row in this.DataSource.Rows)
            {               
                int customerID = NullHandler.GetInt(row["ID"]);
                String customerName = NullHandler.GetString(row["CompanyName"]);
                String logo = NullHandler.GetString(row["Logo"]);
                logo = logo.IsNullOrEmpty() ? String.Empty : String.Format(thumbImage, logo);
                
                String webSite = NullHandler.GetString(row["WebSite"]);
                webSite =  webSite.IsNullOrEmpty() ? String.Empty : String.Format("<b>Web Site: </b><span><a href='{0}' target='_blank'>{1}</a></span>", WebUtil.GetCompleteUrl(webSite), webSite);
                ///TODO: Client Does Not Want website to display
                webSite = String.Empty;

                StringBuilder booter = new StringBuilder(10);
                if (this.IsListOfDevelopers)
                {
                    int projectCount = NullHandler.GetInt(row["ProjectCount"]);
                    String packageName = NullHandler.GetString(row["Package Name"]);
                    booter.AppendFormat("<span>Subscription Type:</span> <b>{0}</b>", packageName);
                    booter.AppendFormat("&nbsp;&nbsp;&nbsp; <span><a href='{0}' target='_blank'>View Projects ({1})</a></span>", GetProjectListURL(customerID), projectCount);
                    if (!webSite.IsNullOrEmpty())
                        booter.Append("<br />");
                }
                booter.Append(webSite);
                String location = NullHandler.GetString(row["City"]).IsNullOrEmpty() ? "N/A" : String.Format("{0}, {1}", NullHandler.GetString(row["City"]).HtmlEncode(), NullHandler.GetString(row["Country"]).HtmlEncode());
                
                html.AppendFormat(listItemHtml, customerID, customerName.HtmlEncode(),
                    location,
                    logo, String.Empty,
                    booter.ToString());                
            }
            //GetDisplayableCustomerInfo(NullHandler.GetString(row["About"]), customerID),;
            ltrListing.Text = html.ToString();
        }
        #endregion New Implementation
    }

    private string GetProjectListURL(int customerID)
    {
        return String.Format("{0}?{1}={2}", AppConstants.Pages.PROJECT_LIST_PUBLIC, AppConstants.QueryString.CUSTOMER_ID, customerID);
    }
    protected String GetDisplayableCustomerInfo(String aboutText, int customerID)
    {
        if (aboutText.WordCount() > ConfigReader.ProjectListDescriptionWordCount)
            return String.Format("{0} &nbsp;&nbsp;<a href='/Pages/Public/CompanyProfile.aspx?ID={1}' target='_blank'>Details</a>", WebUtil.FormatText(aboutText.GetWords(ConfigReader.ProjectListDescriptionWordCount)), customerID);

        return WebUtil.FormatText(aboutText);

    } 
}